
/**
 * Software License, Version 1.0
 *
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 *
 *
 *Redistribution and use in source and binary forms, with or without
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For
 * further information contact the Community Grids Lab at
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and
 * wherever such third-party acknowledgments normally appear.
 *
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering,
 * shall not be used to endorse or promote products derived from this software
 * without prior written permission from Indiana University.  For written
 * permission, please contact the Advanced Research and Technology Institute
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering,
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 *
 *
 * Indiana University provides no reassurances that the source code provided
 * does not infringe the patent or any other intellectual property rights of
 * any other entity.  Indiana University disclaims any liability to any
 * recipient for claims brought by any other entity based on infringement of
 * intellectual property rights or otherwise.
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS.
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS",
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION
 *GENERATED USING SOFTWARE.
 */

/**
 * Implements methods defined in cgl_narada_timer_WindowsClock.h
 *
 * How to compile this code (example)
 * vcvars32.bat
 * cl -Ic:\j2sdk1.4.1\include -Ic:\j2sdk1.4.1\include\win32 -LD WindowsClockImp.cpp -Fehrtimerwin.dll
 *
 * @author Hasan Bulut  - hbulut@indiana.edu
 * Community Grids Lab., Indiana University
 * @version 1.0
 */


#include "stdafx.h"
#include <tchar.h>
#include <windows.h>
#include <WINBASE.H>
#include <tchar.h>
#include <MMSYSTEM.H>
#include "cgl_narada_service_timer_WindowsClock.h"
#include <process.h>

static LARGE_INTEGER freq = {0};
static LARGE_INTEGER initTick = {0};
static LARGE_INTEGER baseTick = {0};
static LARGE_INTEGER baseTimeMillis = {0};
static LARGE_INTEGER baseTimeMicros = {0};
LARGE_INTEGER t_msec = {0};
LARGE_INTEGER t_usec = {0};

/*
 * Class:     cgl_narada_timer_WindowsClock
 * Method:    getTimeMilliseconds
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_cgl_narada_service_timer_WindowsClock_getLocalMilliseconds
(JNIEnv *env, jobject obj) {
    LARGE_INTEGER tick;
    QueryPerformanceCounter(&tick);
    /*
          jlong arr[5];
          for(int i=0;i<5;i++) {
            QueryPerformanceCounter( &tick );
    //        printf("now.tv_sec:%lu now.tv_usec:%lu\n", now.tv_sec, now.tv_usec);
          arr[i] = ((tick.QuadPart + baseTick.QuadPart) * 1000.0)/ freq.QuadPart;
          Sleep(2);
          }
          for(i=0; i<5;i++) {
                  printf("t_msec:%I64u\n", arr[i]);
          }
    //*/
    t_msec.QuadPart = ((tick.QuadPart + baseTick.QuadPart) * 1000.0) / freq.QuadPart;
    return t_msec.QuadPart;

}

/*
 * Class:     cgl_narada_timer_WindowsClock
 * Method:    getTimeMicroseconds
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_cgl_narada_service_timer_WindowsClock_getLocalMicroseconds
(JNIEnv *env, jobject obj) {
    LARGE_INTEGER tick;
    QueryPerformanceCounter(&tick);
    t_usec.QuadPart = ((tick.QuadPart + baseTick.QuadPart) * 1000000.0) / freq.QuadPart;
    return t_usec.QuadPart;
}

/*
 * Class:     cgl_narada_timer_WindowsClock
 * Method:    initValues
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_cgl_narada_service_timer_WindowsClock_initValues
(JNIEnv *env, jobject obj, jlong baseT) {
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); // THREAD_PRIORITY_HIGHEST

    /*
    //(GetCurrentProcess()).PriorityClass = ProcessPriorityClass.RealTime;
    //GetCurrentProcess().Priority = ThreadPriority.Highest;

    SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);


    //*/
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&initTick);
    baseTick.QuadPart = (baseT / 1000.0) * freq.QuadPart - initTick.QuadPart;
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
    printf("freq:     %lu\n", freq.QuadPart);
    printf("resolution: %i nanosec\n", (1000000000 / freq.QuadPart));
    printf("initTick: %I64u\n", initTick.QuadPart);
    printf("baseTick: %I64u\n", initTick.QuadPart);

}

/*
 * Class:     cgl_narada_timer_UnixClock
 * Method:    getTimeMilliseconds
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_cgl_narada_service_timer_WindowsClock_sleep
(JNIEnv *env, jobject obj, jint sleepVal) {
    //SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    //SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); // THREAD_PRIORITY_HIGHEST
    Sleep(sleepVal);
    return;
}



